from django.http import HttpResponse
from django.shortcuts import render
from django.utils.decorators import method_decorator
from django.views import View


# Create your views here.

# GET /template_demo/
def template_demo(request):
    """演示模板加载"""
    context = {
        "name": "xiaohuihui",
        "alist": [2, 1, 5],
        "adict": {"age", 25}
    }
    return render(request, "index.html", context=context)


# 定义装饰器装饰视图
def my_decorator(view_func):
    """定义装饰器装饰视图"""

    def warpper(request, *args, **kwargs):

        print('装饰器被调用了')
        print(request.method)

        # 调用被装饰的视图
        return view_func(request, *args, **kwargs)

    return warpper


# method_decorator(要被转换装饰器, name=要装饰的方法) 如果写在类的上面时,name参数必须指定,如果写在内部方法上时,name不需要指定
# @method_decorator(my_decorator, name='get')
class DemoView(View):
    """类视图"""

    @method_decorator(my_decorator)
    def get(self, request):
        """get请求业务逻辑"""
        return HttpResponse('get请求业务逻辑')

    def post(self, request):
        """post请求业务逻辑"""
        return HttpResponse('post请求业务逻辑')